package controllayer;

import java.util.ArrayList;
import java.util.Date;

import modellayer.Invoice;
import modellayer.Sale;
import dblayer.DBConnection;
import dblayer.DBInvoice;
import dblayer.IFDBInvoice;

public class CtrInvoice {

	private IFDBInvoice dbObj;
	
	public CtrInvoice(){
		dbObj = new DBInvoice();
	}
	
	public ArrayList<Invoice> getAllInvoice(){
		ArrayList<Invoice> list = new ArrayList<Invoice>();
		list = dbObj.getAllInvoices();
		return list;
	}
	
	public Invoice searchByNumber(int number){
		return dbObj.searchByNo(number);
	}
	
	//
	
	public int createInvoice(int number, Date date, double total, Sale sale){
		Invoice invoice = new Invoice(number, date, total, sale);
		
		return dbObj.updateInvoice(invoice);
	}
	
	public void insertInvoice(int number, Date date, double total, Sale sale){
		Invoice invoice = new Invoice(number, date, total, sale);
		
		try{
			DBConnection.startTransaction();
			dbObj.insertInvoice(invoice);
			DBConnection.commitTransaction();
		}
		catch(Exception e){
			DBConnection.rollbackTransaction();
		}
	}
	
	public int deleteInvoice(int number){
		return dbObj.deleteInvoice(number);
	}
}
